package zpin.action;


import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import zpin.dao.AuthorizationDao;
import zpin.dao.CityDao;
import zpin.dao.CompanyDao;
import zpin.dao.EngageDao;
import zpin.dao.PersonDao;
import zpin.dao.ResumeDao;
import zpin.dao.RoleDao;
import zpin.dao.TypeDao;
import zpin.model.Authorization;
import zpin.model.Company;
import zpin.model.Person;
import zpin.model.Role;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Person person;
	
	private Company company;
	
	private Authorization authorization;
	
	private PersonDao personDao;
	
	private CompanyDao companyDao;
	
	private AuthorizationDao authorizationDao;
	
	private CityDao cityDao;
	
	private EngageDao engageDao;
	
	private ResumeDao resumeDao;
	
	private RoleDao roleDao;
	
	private TypeDao typeDao;
	
	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public EngageDao getEngageDao() {
		return engageDao;
	}

	public void setEngageDao(EngageDao engageDao) {
		this.engageDao = engageDao;
	}

	public ResumeDao getResumeDao() {
		return resumeDao;
	}

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public TypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDao typeDao) {
		this.typeDao = typeDao;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Authorization getAuthorization() {
		return authorization;
	}

	public void setAuthorization(Authorization authorization) {
		this.authorization = authorization;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public AuthorizationDao getAuthorizationDao() {
		return authorizationDao;
	}

	public void setAuthorizationDao(AuthorizationDao authorizationDao) {
		this.authorizationDao = authorizationDao;
	}
	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}
	
	public String init(){
		Set<Authorization> authorizations = new HashSet<Authorization>();
		Set<Role> roles = new HashSet<Role>();
		
		Authorization authorization = new Authorization();
		authorization.setAccount("admin");
		authorization.setPassword("password");
		
		Role role = new Role();
		role.setName("个人");
		Role role2 = new Role();
		role2.setName("企业");
		Role role3 = new Role();
		role3.setName("管理员");
		
		roles.add(role3);
		authorization.setRoles(roles);
		roleDao.saveOrUpdate(role);
		roleDao.saveOrUpdate(role2);
		roleDao.saveOrUpdate(role3);
		authorizationDao.saveOrUpdate(authorization);

		return SUCCESS;
	}
	
	public String start(){
		return SUCCESS;
	}
	
	public String personLogin(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		System.out.println("开始注册");
		if(person!=null){
			Set<Role> roles = new HashSet<Role>();
			person.setAuthorization(authorization);
			roles.add(roleDao.findById(1L));
			authorization.setRoles(roles);
			authorizationDao.saveOrUpdate(authorization);
			personDao.saveOrUpdate(person);
			
			session.setAttribute("account", authorization.getAccount());
			session.setAttribute("password", authorization.getPassword());
		}
		
		return SUCCESS;
	}
	
	public String companyLogin(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(company!=null){
			Set<Role> roles = new HashSet<Role>();
			company.setAuthorization(authorization);
			roles.add(roleDao.findById(2L));
			authorization.setRoles(roles);
			authorizationDao.saveOrUpdate(authorization);
			companyDao.saveOrUpdate(company);
			
			session.setAttribute("account", authorization.getAccount());
			session.setAttribute("password", authorization.getPassword());
		}
		return SUCCESS;
	}
	
	public String toPersonLogin(){
		return SUCCESS;
	}
	
	public String toCompanyLogin(){
		return SUCCESS;
	}
	
	public String personLoad(){
		System.out.println("开始登陆");
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(authorizationDao.match(authorization)){
			session.setAttribute("account", authorization.getAccount());
			session.setAttribute("password", authorization.getPassword());
			System.out.println("查看session中key是account的值"+session.getAttribute("account"));
			System.out.println("查看session中key是password的值"+session.getAttribute("password"));
			return SUCCESS;
		}
		else{
			this.addFieldError("errorlabel", "账号密码错误");
			return "error";
		}
	}
	
	public String companyLoad(){
		System.out.println("开始登陆");
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(authorizationDao.match(authorization)){
			session.setAttribute("account", authorization.getAccount());
			session.setAttribute("password", authorization.getPassword());
			System.out.println("查看session中key是account的值"+session.getAttribute("account"));
			System.out.println("查看session中key是password的值"+session.getAttribute("password"));
			return SUCCESS;
		}
		else{
			this.addFieldError("errorlabel", "账号密码错误");
			return "error";
		}
	}
	
	public String toPersonLoad(){
		return SUCCESS;
	}
	
	public String toCompanyLoad(){
		return SUCCESS;
	}
	
	public String function(){
		return SUCCESS;
	}
	
	public String exit(){
		System.out.println("开始退出");
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("account");
		session.removeAttribute("password");
		return SUCCESS;
	}
}
